﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Utils;
using AlipayClass;
using tenpay;

public partial class order_tenpay_Reg : BasePage
{


    private Maticsoft.Model.Order order = new Maticsoft.Model.Order();
    private Maticsoft.BLL.Order orders = new Maticsoft.BLL.Order();
    private Maticsoft.Model.System system = new Maticsoft.Model.System();
    private Maticsoft.BLL.System systems = new Maticsoft.BLL.System();
    private Maticsoft.BLL.UserInfo uifo = new Maticsoft.BLL.UserInfo();
    private Maticsoft.BLL.UserInfo uifos = new Maticsoft.BLL.UserInfo();
    private string user = HttpContext.Current.User.Identity.Name.ToString();

    // Methods
    public static string getRealIp()
    {
        if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
        {
            return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
        }
        return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        if ((base.Request.Form["out_trade_no"] != null) && (base.Request.Form["out_trade_no"].ToString() != ""))
        {
            this.system = this.systems.GetModel(1);
            if ((((this.system != null) && (base.Request.Form["total_fee"] != null)) && ((this.system.tenpaymid != "") && (this.system.tenpaysec != ""))) && (base.Request.Form["total_fee"].ToString() != ""))
            {
                this.setName(this.system.tenpaymid, this.system.tenpaysec, base.Request.Form["out_trade_no"].ToString(), Convert.ToInt32((decimal)(Convert.ToDecimal(base.Request.Form["total_fee"].ToString()) * 100M)).ToString());
            }
        }
    }

    private void setName(string bargainor_id1, string key1, string payId, string money)
    {
        string parameterValue = bargainor_id1;
        string key = key1;
        string str3 = DateTime.Now.ToString("yyyyMMdd");
        string str4 = DateTime.Now.ToString("HHmmss") + TenpayUtil.BuildRandomStr(4);
        string str5 = payId;
        string str6 = parameterValue + str3 + str4;
        string str7 = base.WWWprefix + "order/tenpay/callback.aspx";
        PayRequestHandler handler = new PayRequestHandler(this.Context);
        handler.setKey(key);
        handler.init();
        handler.setParameter("bargainor_id", parameterValue);
        handler.setParameter("sp_billno", str5);
        handler.setParameter("spbill_create_ip", getRealIp());
        handler.setParameter("transaction_id", str6);
        handler.setParameter("return_url", str7);
        handler.setParameter("desc", Helper.SubString(Helper.GetString(base.Request["subject"], string.Empty), 0x20));
        handler.setParameter("total_fee", money);
        handler.setParameter("cs", "utf-8");
        string url = handler.getRequestURL();
        base.Response.Redirect(url);
    }


}